#include <algorithm>
#include <iostream>
#include <iomanip>
#include <sstream>
#include <string>
#include <list>
#include <bitset>
#include <vector>
#include <stack>
#include <queue>
#include <deque>
#include <set>
#include <map>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <cctype>
#include <cmath>
#include <cassert>
#include <ctime>
#include <numeric>
#include <functional>
#include <valarray>
#include <complex>
#include <utility>
using namespace std;

const int dxm[]={-2,-2,-1,-1,1,1,2,2};
const int dym[]={-1,1,-2,2,-2,2,-1,1};
const int dx[]={0,-1,0,1,-1,1, 1,-1};
const int dy[]={1,0,-1,0, 1,1,-1,-1};
const double eps=1e-8;

typedef long long LL;
typedef unsigned long long uLL;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<vi> vgraph;
typedef vector<pii> vpii;
typedef vector<LL> vLL;
typedef vector<string> vs;

#define sz(a) (int)a.size()
#define fori(i,a,b) for(int i(a),_b(b);i<=_b;++i)
#define ford(i,a,b) for(int i(a),_b(b);i>=_b;--i)
#define forn(i,n) fori(i,0,n-1)
#define fora(i,a) forn(i,sz(a))
#define fore(it,c) for(typeof((c).begin()) it=(c).begin();it!=(c).end();++it)
#define all(a) a.begin(),a.end()
#define mp make_pair
#define pb push_back
#define fi first
#define se second
#define clear0(a) memset(a,0,sizeof(a))
#define clearm1(a) memset(a,-1,sizeof(a))
#define maximum(a) (*max_element(all(a)))
#define minimum(a) (*min_element(all(a)))
#define findx(a,x) (find(all(a),x)-a.begin())
#define two(X) (1LL<<(X))
#define contain(S,X) ((S)&two(X))
#define setbit(S,X) ((S)|=two(X))
#define clearbit(S,X) ((S)&=~two(X))
#define togglebit(S,X) ((S)^=two(X))
#define sqr(a) ((a)*(a))
#define finp "input.inp"
#define fout "output.out"
#define nmax 40
#define mmax 26
#define kmax 10
#define modulo 1000000007
#define maxc 999999999

#define debugprint(a) cout << a << ' '
template <typename T>
void debugprintarr(T arr,int x, int y)
{
    fori(i,x,y) debugprint(arr[i]);
    cout << '\n';
}

template <typename T>
void debugprint2darr(T arr, int x1, int y1, int x2, int y2)
{
    fori(i,x1,y1) debugprintarr(arr[i],x2,y2);
}

int n,m;
pii knight[nmax*mmax+1];
bool chessboard[nmax][mmax];
string s;

int kingdist(pii king,int i,int j)
{
    int a=abs(king.fi-i);
    int b=abs(king.se-j);
    return max(a,b);
}
bool inside(pii u)
{
    return u.fi>=0 && u.fi<n && u.se>=0 && u.se<m;
}
void trail(int x,int y,vpii trace[nmax][mmax])
{
    if (chessboard[x][y]) return;
    chessboard[x][y]=true;
    fora(i,trace[x][y])
        trail(trace[x][y][i].fi,trace[x][y][i].se,trace);
}
int bfs(pii center,int cnt)
{
    bool fr[nmax][mmax];
    int d[nmax][mmax];
    vpii trace[nmax][mmax];
    forn(i,n)
    forn(j,m)
    {
        fr[i][j]=true;
        d[i][j]=maxc;
        chessboard[i][j]=false;
    }
    d[center.fi][center.se]=0;
    queue<pii> q;
    q.push(center);
    fr[center.fi][center.se]=false;
    while (!q.empty())
    {
        pii u=q.front();
        q.pop();
        forn(i,8)
        {
            pii v;
            v.fi=u.fi+dxm[i];
            v.se=u.se+dym[i];
            if (inside(v))
            {
                if (fr[v.fi][v.se])
                {
                    d[v.fi][v.se]=d[u.fi][u.se]+1;
                    fr[v.fi][v.se]=false;
                    q.push(v);
                }
                if (d[v.fi][v.se]==d[u.fi][u.se]+1)
                    trace[v.fi][v.se].pb(u);
            }
        }
    }
    int sum=0;
    fori(i,1,cnt-1)
    {
        if (d[knight[i].fi][knight[i].se]==maxc) return maxc;
        sum+=d[knight[i].fi][knight[i].se];
        trail(knight[i].fi,knight[i].se,trace);
    }
    return sum;
}
int bfs_cb(pii king)
{
    int kq=maxc;
    forn(i,n)
        forn(j,m)
        if (chessboard[i][j])
            kq=min(kq,kingdist(king,i,j));
    return kq;
}
int main()
{
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	#ifndef ONLINE_JUDGE
		freopen(finp,"r",stdin);
		//freopen(fout,"w", stdout);
	#endif
    int cnt=0;
    cin >> n >> m;
    while (!cin.eof())
    {
        char c; int x;
        cin >> c >> x;
        knight[cnt].fi=x-1;
        knight[cnt++].se=c-'A';
    }
    cnt--;
    int kq=maxc;
    forn(i,n)
    forn(j,m)
    {
        int sum=bfs(mp(i,j),cnt);
        int dist=bfs_cb(knight[0]);
        if (sum+dist<kq) kq=sum+dist;
    }
    if (cnt==1) kq=0;
    cout << kq;
	return 0;
}
